package com.bobo.dao.tag;

import com.bobo.dao.BaseDao;
import com.bobo.model.Category;
import com.bobo.model.Tag;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by bobo on 2016/10/17.
 */
public class TagDaoImpl extends BaseDao<Tag> implements ITagDao {
    @Override
    public Integer create(Tag tag) {
        String sql = "INSERT INTO t_tag(name,category_id) VALUES(?,?)";
        return create(sql, tag.getName(), tag.getCategory().getId());
    }

    @Override
    public boolean update(Tag tag) {
        String sql = "UPDATE t_tag t SET t.name=? ,t.category_id=?,t.status=? WHERE t.id=?";
        return update(sql, tag.getName(), tag.getCategory().getId(), tag.getStatus(),tag.getId());
    }

    @Override
    public Tag getById(Integer id) {
        String sql = "SELECT  t.id tid,t.name tname,t.`status` ts," +
                "c.`name` cname ,c.id cid " +
                " FROM t_tag t,t_category c WHERE t.`id`=? AND t.`category_id`=c.id";
        Tag tag = new Tag();
        ResultSet rs = getObject(sql, id);
        try {
            while (rs.next()) {
                tag.setId(id);
                tag.setName(rs.getString("tname"));
                tag.setStatus(rs.getInt("ts"));
                Category category = new Category();
                category.setId(rs.getInt("cid"));
                category.setName(rs.getString("cname"));
                tag.setCategory(category);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tag;
    }

    @Override
    public List<Tag> getTags(Integer categoryId,Integer status) {
        String sql = "SELECT  id,name,status FROM t_tag WHERE category_id= ?";
        List<Tag> tags = new ArrayList<>();
        if(status!=null){
            sql = sql+" and status="+status;
        }
        ResultSet rs = getObject(sql, categoryId);
        try {
            while (rs.next()) {
                Tag tag = new Tag();
                tag.setId(rs.getInt("id"));
                tag.setName(rs.getString("name"));
                tag.setStatus(rs.getInt("status"));
                tags.add(tag);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tags;
    }

    @Override
    public List<Tag> getTags(Integer current, Integer pageSize, Integer status) {
        String sql = "SELECT  t.id tid,t.name tname,t.`status` ts,c.`name` cname " +
                " FROM t_tag t,t_category c WHERE t.`category_id`=c.id and t.status=?";
        List<Tag> tags = new ArrayList<>();
        ResultSet rs = pager(sql, current, pageSize, status);
        try {
            while (rs.next()) {
                Tag tag = new Tag();
                tag.setId(rs.getInt("tid"));
                tag.setName(rs.getString("tname"));
                tag.setStatus(rs.getInt("ts"));
                Category category = new Category();
                category.setName(rs.getString("cname"));
                tag.setCategory(category);
                tags.add(tag);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tags;
    }

    @Override
    public Integer getCount(Integer status) {
        String sql = "SELECT  t.id tid,t.name tname,t.`status` ts,c.`name` cname " +
                "FROM t_tag t,t_category c WHERE t.`category_id`=c.id " +
                "ORDER BY t.`category_id`";
        return getCount(sql, status);
    }
}
